import 'dart:io';
import 'package:bot_toast/bot_toast.dart';
import 'package:example/utils/common.dart';
import 'package:example/view/error/page_not_found/page_not_found_view.dart';
import 'package:example/utils/local_manage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:get/get_navigation/src/routes/get_route.dart';
import 'package:intl/intl.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'base/base_theme_data.dart';
import 'base/custom_navigator_observer.dart';
import 'config/binding_config.dart';
import 'config/http_config.dart';
import 'config/route_config.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Intl.defaultLocale = "zh_CN";
  // JsonTools.log = (text) => loggerJson.i(text);
  await LocalManage().init();
  HttpConfig.init();
  //竖屏
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  runApp(const MyApp());

  /// android 状态栏透明
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      builder: (c,w) => RefreshConfiguration(
        headerBuilder: () => const ClassicHeader(
          idleText: '下拉刷新',
          releaseText: "松手即可刷新",
          refreshingText: "努力加载数据",
          completeText: "加载完成",
          failedText: "加载失败",
        ),
        // 配置默认头部指示器,假如你每个页面的头部指示器都一样的话,你需要设置这个
        footerBuilder: () => const ClassicFooter(
          idleText: '上拉加载',
          loadingText: "加载中",
          noDataText: "没有更多了",
          failedText: "加载失败请重试",
          canLoadingText: '上拉加载',
        ),
        //无数据可以上拉加载
        enableLoadingWhenNoData: true,
        // 配置默认底部指示器
        headerTriggerDistance: 80.0,
        // 头部触发刷新的越界距离
        springDescription:
            const SpringDescription(stiffness: 170, damping: 16, mass: 1.9),
        // 自定义回弹动画,三个属性值意义请查询flutter api
        maxOverScrollExtent: 100,
        //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
        maxUnderScrollExtent: 0,
        // 底部最大可以拖动的范围
        enableScrollWhenRefreshCompleted: true,
        //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
        enableLoadingWhenFailed: true,
        //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
        hideFooterWhenNotFull: true,
        // Viewport不满一屏时,禁用上拉加载更多功能
        enableBallisticLoad: true,
        shouldFooterFollowWhenNotFull: (state) => false,
        // 可以通过惯性滑动触发加载更多
        child: GetMaterialApp(
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('zh', 'CN'),
          ],
          title: '中商数据采集系统',
          navigatorKey: globalKey,
          initialBinding: BindingConfig(),
          initialRoute: RouteConfig.main,
          getPages: RouteConfig.getPages,
          unknownRoute: GetPage(
            name: RouteConfig.pageNotFoundError,
            page: () => PageNotFoundPage(),
          ),
          debugShowCheckedModeBanner: false,
          builder: (context, child) => GestureDetector(
            onTap: () {
              hideKeyboard();
            },
            child: MediaQuery(
              data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
              child: BotToastInit()(context, child),
            ),
          ),
          navigatorObservers: [
            BotToastNavigatorObserver(),
            CustomNavigatorObserver()
          ],
          //2.注册路由观察者
          theme: getTheme(),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    // NetworkManager.getInstance().connect();
  }

  @override
  void dispose() {
    super.dispose();
    // NetworkManager.getInstance().disconnect();
  }
}
